package com.example.xmpic.support.config;

import com.example.xmpic.support.FunLog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author skygge
 * @date 2020/7/1.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class FbExtraStateQuery extends BaseConfig{

    public static final String CONFIG_NAME = JsonConfig.CFG_FbExtraStateCtrl;

    private int isStateOn;

    public int getIsStateOn() {
        return isStateOn;
    }

    @Override
    public String getConfigName() {
        return CONFIG_NAME;
    }

    public String getConfigNameOfChannel(){
        return Config_Name_ofchannel;
    }

    @Override
    public boolean onParse(String json) {
        if ( !super.onParse(json) ) {
            return false;
        }

        try {
            Config_Name_ofchannel = mJsonObj.getString("Name");
            JSONObject c_jsonobj = mJsonObj.getJSONObject(mJsonObj.getString("Name"));
            return onParse(c_jsonobj);
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean onParse(JSONObject obj) throws JSONException {
        if (null == obj) {
            return false;
        }

        isStateOn = obj.optInt("ison");

        return true;
    }

    public void setIsStateOn(Boolean isStateOn) {
        if ( isStateOn ) {
            this.isStateOn = 1;
        } else {
            this.isStateOn = 0;
        }
    }

    @Override
    public String getSendMsg() {
        super.getSendMsg();
        try {
            mJsonObj.put("Name", getConfigNameOfChannel());
            mJsonObj.put("SessionID", "0x00001234");

            JSONObject c_jsonObj = null;
            if ( mJsonObj.isNull(getConfigNameOfChannel()) ) {
                c_jsonObj = new JSONObject();
            } else {
                c_jsonObj = mJsonObj.getJSONObject(getConfigNameOfChannel());
            }

            c_jsonObj.put("ison", isStateOn);

            mJsonObj.put(getConfigNameOfChannel(), c_jsonObj);

        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        FunLog.d(getConfigNameOfChannel(), "json:" + mJsonObj.toString());
        return mJsonObj.toString();
    }
}
